<?php

namespace Admin\Model;

use Think\Model;

class GuestbookModel extends Model {

	/**
	 * 保存数据
	 *
	 * @param unknown $data
	 * @return string
	 */
	public function saveData($data) {
		if (! $data ['contact']) {
			$result ['status'] = 0;
			$result ['msg'] = '联系方式不能空';
		} elseif (! $data ['context']) {
			$result ['status'] = 0;
			$result ['msg'] = '反馈内容不能空';
		} else {
			$data['uid'] = get_cookie('uid');
			$data ['createtime'] = date ( 'Y-m-d H:i:s' );
			$data ['createip'] = get_user_ip ();
			$rs = $this->add ( $data );
			if ($rs) {
				$result ['status'] = 1;
				$result ['msg'] = '添加成功';
				$result ['id'] = $rs;
			} else {
				$result ['status'] = 0;
				$result ['msg'] = '添加失败';
			}
		}
		return $result;
	}

	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = 12) {
		$page = intval ( $page ) < 1 ? 1 : $page;
		$result ['list'] = $this->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		return $result;
	}

	/**
	 * 删除操作(non-PHPdoc)
	 *
	 * @see \Think\Model::delete()
	 */
	public function delete($id) {
		$result ['msg'] = '删除失败';
		$result ['status'] = 0;
		if ($id) {
			// 检查是否有子信息

			// 删除
			$rs = M ( 'site' )->where ( 'id=' . intval ( $id ) )->delete ();
			;

			$result ['msg'] = '删除成功';
			$result ['status'] = 1;
		} else {
			$result ['msg'] = '删除失败';
			$result ['status'] = 0;
		}
		return $result;
	}
}
